package com.hhh.tools.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hhh.tools.model.entity.GoodsInfoBean
import com.hhh.tools.model.service.CommodityService

class CommodityViewModel : ViewModel() {

    private val commodityService = CommodityService.instance()

    private val size = 10
    private var page = 1

    var list by mutableStateOf(
        listOf<GoodsInfoBean>()
    )

    suspend fun loadCommodityList() {
        val map = HashMap<String, String>()
        map["type"] = "0"
        map["page"] = page.toString()
        map["size"] = size.toString()
        val res = commodityService.loadGoodsList(map)
        Log.e("hhh", "res= ${res.data}")
        val tmpList = mutableListOf<GoodsInfoBean>()
        if (page == 1) {
            tmpList.clear()
        }
        res.data?.let { tmpList.addAll(it) }
        list = tmpList
    }
}